#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>
#include <QScreen>

#include "BaseControl/Slider/MCommonSlider.h"
#include "BaseControl/Slider/MLadderStepSlider.h"
#include "BaseControl/ToolTip/MGlobalToolTip.h"
#include "BaseControl/ToolTip/MResidentAlert.h"
#include "BaseControl/ToolTip/MTriangleToolTip.h"

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    ui->verticalLayout_2->setAlignment(Qt::AlignTop | Qt::AlignHCenter);
    ui->verticalLayout->setAlignment(Qt::AlignTop | Qt::AlignHCenter);

    mtGlobalMsg_ = new MGlobalToolTip(MGlobalToolTip::LEVEL_FORTIFIED, MGlobalToolTip::ICON_INFO, MGlobalToolTip::STYLE_CHILD, this);
    mtGlobalMsg_->setMsgContent(tr("通知文案提示"));

    mResidentToolTip_ = new MResidentAlert(MResidentAlert::ICON_INFO, MResidentAlert::STYLE_CHILD, this);
    mResidentToolTip_->setMsgContent(tr("通知文案提示"));

    auto *hideMsgFrame = new QFrame(this);
    hideMsgFrame->setFixedHeight(100);

    auto *animationMsgFrame = new QFrame(this);
    animationMsgFrame->setFixedHeight(100);

    mCommonSlider_ = new MCommonSlider(this);
    mCommonSlider_->setFixedHeight(100);
    mCommonSlider_->setRange(-100, 100);
    mCommonSlider_->setBgStyle(QColor(0, 0, 0, 200), MCommonSlider::STYLE_NORMAL);
    //    mCommonSlider_->setGrooveBgStyle(2, 1, {QColor(255, 255, 255, 38)}, MCommonSlider::STYLE_NORMAL);
    mCommonSlider_->setGrooveBgStyle(2, 1, {QColor(95, 95, 96, 200), QColor(157, 132, 69, 200), QColor(238, 82, 33, 200)}, MCommonSlider::STYLE_NORMAL);
    //    mCommonSlider_->setSubPageBgStyle(QColor(217, 217, 217, 200), MCommonSlider::STYLE_NORMAL);
    mCommonSlider_->setSubPageBgStyle({QColor(40, 179, 172, 200), QColor(172, 181, 70, 200)}, MCommonSlider::STYLE_NORMAL);
    mCommonSlider_->setHandleBgStyle(10, 10, 5, QColor(0, 0, 0, 200), MCommonSlider::STYLE_NORMAL);
    mCommonSlider_->setHandleBorderStyle(1, QColor(255, 255, 255, 200), MCommonSlider::STYLE_NORMAL);

    mCommonSlider_->setBgStyle(QColor(0, 0, 0, 200), MCommonSlider::STYLE_HOVER);
    //    mCommonSlider_->setGrooveBgStyle(3, 1, {QColor(255, 255, 255, 38)}, MCommonSlider::STYLE_HOVER);
    mCommonSlider_->setGrooveBgStyle(2, 1, {QColor(95, 95, 96), QColor(157, 132, 69), QColor(238, 82, 33)}, MCommonSlider::STYLE_HOVER);
    //    mCommonSlider_->setSubPageBgStyle(QColor(255, 255, 255), MCommonSlider::STYLE_HOVER);
    mCommonSlider_->setSubPageBgStyle({QColor(40, 179, 172), QColor(172, 181, 70)}, MCommonSlider::STYLE_HOVER);
    mCommonSlider_->setHandleBgStyle(10, 10, 5, QColor(0, 0, 0), MCommonSlider::STYLE_HOVER);
    mCommonSlider_->setHandleBorderStyle(1, QColor(255, 255, 255), MCommonSlider::STYLE_HOVER);

    mCommonSlider_->setBgStyle(QColor(0, 0, 0, 50), MCommonSlider::STYLE_DISABLE);
    //    mCommonSlider_->setGrooveBgStyle(2, 1, {QColor(255, 255, 255, 10)}, MCommonSlider::STYLE_DISABLE);
    mCommonSlider_->setGrooveBgStyle(2, 1, {QColor(95, 95, 96, 50), QColor(157, 132, 69, 200), QColor(238, 82, 33, 50)}, MCommonSlider::STYLE_DISABLE);
    //    mCommonSlider_->setSubPageBgStyle(QColor(255, 255, 255, 50), MCommonSlider::STYLE_DISABLE);
    mCommonSlider_->setSubPageBgStyle({QColor(40, 179, 172, 50), QColor(172, 181, 70, 50)}, MCommonSlider::STYLE_DISABLE);
    mCommonSlider_->setHandleBgStyle(10, 10, 5, QColor(0, 0, 0, 50), MCommonSlider::STYLE_DISABLE);
    mCommonSlider_->setHandleBorderStyle(1, QColor(255, 255, 255, 150), MCommonSlider::STYLE_DISABLE);
    mCommonSlider_->setEnableHandleAnimation(true, 4);
    //    QFont font("Microsoft YaHei");
    //    font.setStyleStrategy(QFont::PreferAntialias);
    //    font.setHintingPreference(QFont::PreferNoHinting);
    //    mCommonSlider_->setText("mCommonSlider_", MCommonSlider::ALIGN_LEFT | MCommonSlider::ALIGN_VCENTER);
    //    mCommonSlider_->setTextStyle(font, QColor(255,0,0,200), MCommonSlider::STYLE_NORMAL);
    //    mCommonSlider_->setTextStyle(font, QColor(255,0,0), MCommonSlider::STYLE_HOVER);
    //    mCommonSlider_->setTextStyle(font, QColor(255,0,0,50), MCommonSlider::STYLE_DISABLE);

    MLadderStepSlider_ = new MLadderStepSlider(this);
    MLadderStepSlider_->setFixedHeight(100);
    MLadderStepSlider_->setRange(0, 12000);
    MLadderStepSlider_->setBgStyle(QColor(0, 0, 0, 200), MLadderStepSlider::STYLE_NORMAL);
    MLadderStepSlider_->setEnableHandleAnimation(true, 4);
    std::vector<std::tuple<int, int, int, int>> rulesTuple = {
        {1, 10, 1, 5}, {10, 50, 5, 5}, {50, 100, 10, 5}, {100, 200, 25, 5}, {200, 300, 50, 10}, {300, 5000, 100, 30}, {5000, 12000, 200, 40},
    };
    MLadderStepSlider_->setStepRules(rulesTuple);

    connect(mCommonSlider_, &MCommonSlider::valueChanging, [this](int value) {
        ui->lineEdit->setText(QString("changing %1").arg(value));
        qDebug() << "changing " << value;
    });
    connect(mCommonSlider_, &MCommonSlider::valueChanged, [this](int value) {
        ui->lineEdit->setText(QString("changed %1").arg(value));
        qDebug() << "changed " << value;
    });

    connect(MLadderStepSlider_, &MLadderStepSlider::valueChanging, [this](int value) {
        ui->lineEdit_2->setText(QString("changing %1").arg(value));
        qDebug() << "changing " << value;
    });
    connect(MLadderStepSlider_, &MLadderStepSlider::valueChanged, [this](int value) {
        ui->lineEdit_2->setText(QString("changed %1").arg(value));
        qDebug() << "changed " << value;
    });
    connect(ui->lineEdit_3, &QLineEdit::editingFinished, [this]() {
        MLadderStepSlider_->setValue(ui->lineEdit_3->text().toInt());});

    ui->verticalLayout_2->addWidget(mtGlobalMsg_);
    ui->verticalLayout_2->addWidget(mResidentToolTip_);
    ui->verticalLayout_2->addWidget(hideMsgFrame);
    ui->verticalLayout_2->addWidget(animationMsgFrame);
    ui->verticalLayout_2->addWidget(mCommonSlider_);
    ui->verticalLayout_2->addWidget(MLadderStepSlider_);

    mtGlobalMsgAutoHidden_ = new MGlobalToolTip(MGlobalToolTip::LEVEL_FORTIFIED, MGlobalToolTip::ICON_INFO, MGlobalToolTip::STYLE_CHILD, hideMsgFrame);
    // connect(mtGlobalMsgAutoHidden_, &MGlobalToolTip::sgnAutoHidden, [this]() { mtGlobalMsgAutoHidden_->deleteLater(); });
    mtGlobalMsgAutoHidden_->setDwellTime();
    mtGlobalMsgAutoHidden_->setMsgContent(tr("通知"));
    mtGlobalMsgAutoHidden_->hide();

    mtGlobalMsgAnimation_ = new MGlobalToolTip(MGlobalToolTip::LEVEL_FORTIFIED, MGlobalToolTip::ICON_INFO, MGlobalToolTip::STYLE_CHILD, animationMsgFrame);
    // connect(mtGlobalMsgAnimation_, &MGlobalToolTip::sgnAutoHidden, [this]() { mtGlobalMsgAnimation_->deleteLater(); });
    mtGlobalMsgAnimation_->setMsgContent(tr("通知"));
    mtGlobalMsgAnimation_->setDwellTime();
    mtGlobalMsgAnimation_->hide();

    //
    mTriangleToolTip_ = new MTriangleToolTip(ui->pushButton_5);
    mTriangleToolTip_->setToolTipContent(tr("Learn more please visit: <a style=color:#32bc84; href = http://www.baidu.com>www.baidu.com</a>"), true);
    //    mTriangleToolTip_->setToolTipContent(tr("通知文案提示通知文案提示通知文案提示通知文案提示通知文案提示通知文案提示通知文案提示通知文案提示通知文案提示"),
    //    false); mTriangleToolTip_->setToolTipContent(tr("通知\n通知\n通知"), false);

    //    ui->verticalLayout_2->addWidget(mTriangleToolTip_);

    //    ui->pushButton_6->setTextContent("通知文案提示");
    //    ui->pushButton_6->setIconIndex(YunXiuIconFont::mIconAddPicture, 30, 25);
    //    ui->pushButton_6->setTextNormalStyle(QColor(255, 0, 0));
    //    ui->pushButton_6->setTextHoverStyle(QColor(0, 255, 0));
    //    ui->pushButton_6->setTextDisableStyle(QColor(0, 0, 255));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    static int msgStyleIndex = 0;
    switch (msgStyleIndex)
    {
    case 0:
        msgStyleIndex++;
        mtGlobalMsg_->updateMsgConfig(MGlobalToolTip::LEVEL_REGULAR, MGlobalToolTip::ICON_INFO, 500);
        mtGlobalMsg_->setMsgContent(tr("001 通知\n通知\n通知"));

        mResidentToolTip_->updateMsgConfig(MResidentAlert::ICON_INFO);
        mResidentToolTip_->setMsgContent(tr("001 通知\n通知\n通知"));
        break;
    case 1:
        msgStyleIndex++;
        mtGlobalMsg_->updateMsgConfig(MGlobalToolTip::LEVEL_REGULAR, MGlobalToolTip::ICON_WARNING, 500);
        mtGlobalMsg_->setMsgContent(tr("002 在主界面初始化时，调用这个函数，并把资源文件也就是QSS文件路径传进去："));

        mResidentToolTip_->updateMsgConfig(MResidentAlert::ICON_WARNING);
        mResidentToolTip_->setMsgContent(tr("001 在主界面初始化时，调用这个函数，并把资源文件也就是QSS文件路径传进去："));
        break;
    case 2:
        msgStyleIndex++;
        mtGlobalMsg_->updateMsgConfig(MGlobalToolTip::LEVEL_REGULAR, MGlobalToolTip::ICON_SUCCESSFUL, 100);
        mtGlobalMsg_->setMsgContent(tr("003 在主界面初始化时，调用这个函数，并把资源文件也就是QSS文件路径传进去："));

        mResidentToolTip_->updateMsgConfig(MResidentAlert::ICON_SUCCESSFUL, 200);
        mResidentToolTip_->setMsgContent(tr("001 在主界面初始化时，调用这个函数，并把资源文件也就是QSS文件路径传进去："));
        break;
    case 3:
        msgStyleIndex++;
        mtGlobalMsg_->updateMsgConfig(MGlobalToolTip::LEVEL_REGULAR, MGlobalToolTip::ICON_FAILED, 100);
        mtGlobalMsg_->setMsgContent(tr("004 在主界面初始化时，调用这个函数，并把资源文件也就是QSS文件路径传进去："));

        mResidentToolTip_->updateMsgConfig(MResidentAlert::ICON_FAILED, 200);
        mResidentToolTip_->setMsgContent(tr("001 在主界面初始化时，调用这个函数，并把资源文件也就是QSS文件路径传进去："));
        break;
    case 4:
        msgStyleIndex++;
        mtGlobalMsg_->updateMsgConfig(MGlobalToolTip::LEVEL_FORTIFIED, MGlobalToolTip::ICON_INFO, 100);
        mtGlobalMsg_->setMsgContent(tr("005 在主界面初始化时，调用这个函数，并把资源文件也就是QSS文件路径传进去："));
        break;
    case 5:
        msgStyleIndex++;
        mtGlobalMsg_->updateMsgConfig(MGlobalToolTip::LEVEL_FORTIFIED, MGlobalToolTip::ICON_WARNING, 500);
        mtGlobalMsg_->setMsgContent(R"(006 通知文案提示，通知文案提示)");
        break;
    case 6:
        msgStyleIndex++;
        mtGlobalMsg_->updateMsgConfig(MGlobalToolTip::LEVEL_FORTIFIED, MGlobalToolTip::ICON_SUCCESSFUL, -1);
        mtGlobalMsg_->setMsgContent(R"(007 通知文案提示，通知文案提示)");
        break;
    case 7:
        msgStyleIndex++;
        mtGlobalMsg_->updateMsgConfig(MGlobalToolTip::LEVEL_FORTIFIED, MGlobalToolTip::ICON_FAILED, -1);
        mtGlobalMsg_->setMsgContent(R"(008 通知文案提示，通知文案提示)");
        break;
    default:
        msgStyleIndex = 0;
        break;
    }
}

void MainWindow::on_pushButton_2_clicked()
{
    mtGlobalMsgAutoHidden_->show();
}

void MainWindow::on_pushButton_3_clicked()
{
    mtGlobalMsgAnimation_->hide();
    static int animationIndex = 0;
    switch (animationIndex)
    {
    case 0:
        animationIndex++;
        mtGlobalMsgAnimation_->setIntoAnimation(MGlobalToolTip::ANIMATION_LEFT_TO_RIGHT);
        mtGlobalMsgAnimation_->setExitAnimation(MGlobalToolTip::ANIMATION_LEFT_TO_RIGHT);
        break;
    case 1:
        animationIndex++;
        mtGlobalMsgAnimation_->setIntoAnimation(MGlobalToolTip::ANIMATION_TOP_TO_DOWN);
        mtGlobalMsgAnimation_->setExitAnimation(MGlobalToolTip::ANIMATION_TOP_TO_DOWN);
        break;
    case 2:
        animationIndex++;
        mtGlobalMsgAnimation_->setIntoAnimation(MGlobalToolTip::ANIMATION_RIGHT_TO_LEFT);
        mtGlobalMsgAnimation_->setExitAnimation(MGlobalToolTip::ANIMATION_RIGHT_TO_LEFT);
        break;
    case 3:
        animationIndex++;
        mtGlobalMsgAnimation_->setIntoAnimation(MGlobalToolTip::ANIMATION_DOWN_TO_TOP);
        mtGlobalMsgAnimation_->setExitAnimation(MGlobalToolTip::ANIMATION_DOWN_TO_TOP);
        break;
    default:
        animationIndex = 0;
        mtGlobalMsgAnimation_->setIntoAnimation(MGlobalToolTip::ANIMATION_LEFT_TO_RIGHT);
        mtGlobalMsgAnimation_->setExitAnimation(MGlobalToolTip::ANIMATION_LEFT_TO_RIGHT);
        break;
    }
    mtGlobalMsgAnimation_->show();
}

void MainWindow::on_pushButton_4_clicked()
{
    if (mtGlobalMsgPopup_)
    {
        mtGlobalMsgPopup_->deleteLater();
        mtGlobalMsgPopup_ = nullptr;
    }
    mtGlobalMsgPopup_ = new MGlobalToolTip(MGlobalToolTip::LEVEL_FORTIFIED, MGlobalToolTip::ICON_WARNING, MGlobalToolTip::STYLE_POPUP, this);
    connect(mtGlobalMsgPopup_, &MGlobalToolTip::sgnAutoHidden, [this]() {
        mtGlobalMsgPopup_->deleteLater();
        mtGlobalMsgPopup_ = nullptr;
    });
    mtGlobalMsgPopup_->setDwellTime();
    mtGlobalMsgPopup_->setMsgContent(tr("通知文案提示，通知文案提示"));
    mtGlobalMsgPopup_->setIntoAnimation(MGlobalToolTip::ANIMATION_LEFT_TO_RIGHT);
    mtGlobalMsgPopup_->setExitAnimation(MGlobalToolTip::ANIMATION_LEFT_TO_RIGHT);
    mtGlobalMsgPopup_->move(500, 500);
    mtGlobalMsgPopup_->show();
    //     QTimer::singleShot(3000, [this]() {

    //     });
}

void MainWindow::on_pushButton_5_clicked()
{
    if (mTriangleToolTip_ && mTriangleToolTip_->isVisible())
    {
        mTriangleToolTip_->deleteLater();
        mTriangleToolTip_ = nullptr;
        return;
    }
    mTriangleToolTip_ = new MTriangleToolTip(ui->pushButton_5);
    mTriangleToolTip_->setToolTipPosition(MTriangleToolTip::POS_BOTTOM);

    {

        static int posIndex = 0;
        switch (posIndex)
        {
        case 0:
            mTriangleToolTip_->setToolTipContent(tr("Learn more please visit: <a style=color:#32bc84; href = http://www.baidu.com>www.baidu.com</a>"), true);
            //    mTriangleToolTip_->setToolTipContent(tr("通知\n通知\n通知"), false);
            posIndex++;
            break;
        case 1:
            mTriangleToolTip_->setToolTipContent(tr("通知文案提示通知文案提示通知文案提示通知文案提示通知文案提示通知文案提示通知文案提示通知文案提示通知文案提示"),
                                                 false);
            posIndex++;
            break;
        case 2:
            mTriangleToolTip_->setToolTipContent(tr("通知文案提示\n通知文案提示\n通知文案提示\n通知文案提示"), false);
            posIndex++;
            break;
        case 3:
            mTriangleToolTip_->setToolTipContent(tr("通知"), false);
            posIndex++;
            break;
        default:
            posIndex = 0;
            break;
        }

        //      mTriangleToolTip_->showAndAutoAdjustPos(mapToGlobal(rect.topLeft()), rect.width(), rect.height());
        //        mTriangleToolTip_->showAndAutoAdjustPos(ui->pushButton_5);

        const auto &&width = ui->pushButton_5->geometry().width();
        const auto &&height = ui->pushButton_5->geometry().height();
        const auto &&gPoint = mapToGlobal(ui->pushButton_5->geometry().topLeft());

        const auto &&screen = ui->pushButton_5->screen()->geometry();

        mTriangleToolTip_->showAndAutoAdjustPos(screen, gPoint, width, height);
    }
}

void MainWindow::on_pushButton_7_clicked()
{
    //    ui->pushButton_6->setEnabled(!ui->pushButton_6->isEnabled());
    mCommonSlider_->setEnabled(!mCommonSlider_->isEnabled());
    this->setCursor(mCommonSlider_->isEnabled() ? Qt::PointingHandCursor : Qt::ArrowCursor);
}
